package com.juzi.wopu.service.impl;

import java.util.List;
import java.util.Map;

import com.juzi.wopu.domain.Employee;
import com.juzi.wopu.domain.Menu;
import com.juzi.wopu.service.IMenuService;

public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService{

	@SuppressWarnings("unchecked")
	@Override
	public List<Menu> findByLogin(Employee loginUser) {
		String hql="select distinct m from Employee e join e.roles r join r.menus m where e.id=? and m.parent_id=0";
		List<Menu> parents = BaseDao.findByhql(hql, loginUser.getId());
		for (Menu parent : parents) {
			hql="select distinct m from Employee e join e.roles r join r.menus m where e.id=? and m.parent_id.id=?";
			List<Menu> children = BaseDao.findByhql(hql,loginUser.getId(), parent.getId());
			parent.setChildren(children);
		}
		return parents;
	}

	@Override
	public void getMenuMap(Map<Long, Menu> map1, Map<String, Menu> map2) {
		List<Menu> list=BaseDao.getAll(Menu.class);
		if(list!=null && list.size()>0){
			if(map1!=null && map2==null){
				for(Menu c:list){
					map1.put(c.getId(), c);
				}
			}
			else if(map1==null && map2!=null){
				for(Menu c:list){
					map2.put(c.getName(), c);
				}
			}
			else if(map1!=null && map2!=null){
				for(Menu c:list){
					map1.put(c.getId(), c);
					map2.put(c.getName(), c);
				}
			}
		}
		
	}
}